﻿using System;
using System.Text;

namespace OS.Common.Utils
{
    /// <summary>
    /// 字符串通用功能
    /// </summary>
    public static class StringUtil
    {
        private static readonly Random Random = new Random(DateTime.Now.Millisecond);

        private static readonly char[] ArrayChar = new char[60]
        {
            '0',
            '1',
            '2',
            '3',
            '4',
            '5',
            '6',
            '7',
            '8',
            '9',
            'a',
            'b',
            'd',
            'c',
            'e',
            'f',
            'g',
            'h',
            'i',
            'j',
            'k',
            'l',
            'm',
            'n',
            'p',
            'r',
            'q',
            's',
            't',
            'u',
            'v',
            'w',
            'z',
            'y',
            'x',
            'A',
            'B',
            'C',
            'D',
            'E',
            'F',
            'G',
            'H',
            'I',
            'J',
            'K',
            'L',
            'M',
            'N',
            'Q',
            'P',
            'R',
            'T',
            'S',
            'V',
            'U',
            'W',
            'X',
            'Y',
            'Z'
        };

        private const string ArrayCodeString = "12356789ABCDEFGHJKLMNPQRSTUVWXYZ";

        /// <summary>
        /// 生成随机串
        /// </summary>
        /// <param name="length">字符长度位</param>
        /// <returns></returns>
        public static string RandomString(int length = 8)
        {
            var stringBuilder = new StringBuilder(length);
            for (var i = 0; i < length; i++)
            {
                stringBuilder.Append(ArrayChar[Random.Next(0, 59)]);
            }

            return stringBuilder.ToString();
        }

        /// <summary>
        /// 数字转化为短语
        /// </summary>
        /// <param name="number">数字</param>
        /// <returns></returns>
        public static string ToCode(this long number)
        {
            var stringBuilder = new StringBuilder(13);
            while (number > 0)
            {
                var number2 = number & 0x1F;
                stringBuilder.Append(ArrayCodeString[(int) number2]);
                number >>= 5;
            }

            return stringBuilder.ToString();
        }

        /// <summary>
        /// 使用短语反推数字
        /// </summary>
        /// <param name="code">短语代码</param>
        /// <returns></returns>
        public static long ToCodeNumber(this string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return 0L;
            }

            var length = code.Length;
            if (length > 13)
            {
                throw new ArgumentOutOfRangeException("code", "这个短语代码不是来自[ToCode]方法！");
            }

            var number = 0L;
            for (var number2 = length - 1; number2 >= 0; number2--)
            {
                var number3 = ArrayCodeString.IndexOf(code[number2]);
                if (number3 < 0)
                {
                    throw new ArgumentOutOfRangeException("code", "这个短语代码不是来自[ToCode]方法！");
                }

                number = (number << 5 ^ number3);
            }

            return number;
        }
    }
}